package com.aloda.scoring.impl;

import com.aloda.dto.QuestionAndResultListDTO;
import com.aloda.dto.ScoreAndScoringResultDTO;
import com.aloda.entity.App;
import com.aloda.entity.Question;
import com.aloda.entity.ScoringResult;
import com.aloda.entity.UserAnswer;
import com.aloda.scoring.DataTreating;
import com.aloda.scoring.ScoringStrategy;
import com.aloda.scoring.anno.ScoringStrategyConfig;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * 自定义评分类应用评分策略
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {

    @Resource
    private DataTreating dataTreating;

    /**
     * 执行评分
     *
     * @param choices
     * @param app
     * @return
     * @throws Exception
     */
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        // 1.获取题目和评分结果列表
        QuestionAndResultListDTO questionAndResult = dataTreating.getQuestionAndResult(choices, app);
        Question question = questionAndResult.getQuestion();
        List<ScoringResult> scoringResultList = questionAndResult.getScoringResultList();

        // 2.获取对应的评分结果
        ScoreAndScoringResultDTO userScoreRange = dataTreating.getUserScoreRange(question, choices, scoringResultList);
        Integer scoreSum = userScoreRange.getResultScore();
        ScoringResult result = userScoreRange.getScoringResult();

        // 3.封装用户回答记录
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(choices.toString());
        userAnswer.setResultId(result.getId());
        userAnswer.setResultName(result.getResultName());
        userAnswer.setResultDesc(result.getResultDesc());
        userAnswer.setResultPicture(result.getResultPicture());
        userAnswer.setResultScore(scoreSum);
        return userAnswer;
    }
}
